package com.graduation.system.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import cn.circle.commom.controller.BackendBaseController;

import com.graduation.system.model.SysDepartment;
import com.graduation.system.model.SysLostAndFound;

import cn.circle.system.service.ISysDepartmentService;
import cn.circle.system.service.ISysLostAndFoundService;
import cn.circle.system.util.ConfigProperty;
import cn.circle.system.util.DataGrid;
import cn.circle.system.util.FileUploadUtil;
import cn.circle.system.util.PageHelper;
import cn.circle.system.util.ReturnInfo;

/**
 * 控制器 作者：吴泽磊
 */
@Controller
@EnableWebMvc
@RequestMapping("/manage/sysLostAndFound")
public class SysLostAndFoundController extends BackendBaseController {

	private static final Log log = LogFactory.getLog(SysLostAndFoundController.class);

	@Autowired
	private ISysLostAndFoundService sysLostAndFoundService;
	@Autowired
	private ISysDepartmentService sysDepaermentService;

	/**
	 * 跳转至 sys_lost_and_found页
	 */
	@RequestMapping(value = "/index")
	public String index(Map<String, Object> map) {
		List<SysDepartment> dep = this.getDepartment().getRows();
		map.put("dep", dep);
		return "/WEB-INF/jsp/system/system/lost_and_found/sys_lost_and_found_list.jsp";
	}

	/**
	 * 查询客户信息
	 */
	@ResponseBody
	@RequestMapping(value = "/dataGrid")
	public DataGrid dataGrid(@ModelAttribute SysLostAndFound c, String arcitleName, String depId, PageHelper ph,HttpServletRequest request) {
		
		ph.setSort("arcitleId");
		ph.setOrder("desc");
		DataGrid dg = sysLostAndFoundService.datagrid(ph,c,depId,arcitleName);
		
		return dg;
	}
	/**
	/**
	 * 跳转至 sys_lost_and_found_au页(添加和修改共用一个编辑页)
	 */
	
	@RequestMapping(value = "/add")
	public String add(Map<String, Object> map) {
		log.info("----Ok----");
		map.put("saveUrl", "/manage/sysLostAndFound/addSave");
		List<SysDepartment> dep = this.getDepartment().getRows();
		map.put("dep", dep);
		return "/WEB-INF/jsp/system/system/lost_and_found/sys_lost_and_found_au.jsp";
	} 

	/**
	 * 保存添加数据
	 */
	
	@ResponseBody
	@RequestMapping(value = "/addSave")
	public ReturnInfo addSave(@Valid SysLostAndFound sysLostAndFound, BindingResult b, ReturnInfo result, @RequestParam(value = "uploadFile", required = false) MultipartFile file, HttpServletRequest request) {
	if (b.getErrorCount() > 0) { // 数据校验
		for (FieldError error : b.getFieldErrors()) {	
			log.info(error.getField() + ":" + error.getDefaultMessage());
				result.setMsg(error.getField() + "" + error.getDefaultMessage());
		}
		result.setSuccess(1);
	} else {
		if(file != null){
			try{
				System.out.println("ggg");
				String icon_url = FileUploadUtil.saveFile(file, request, ConfigProperty.getProperty("fileUplaodPath", "config") + "icon");
				sysLostAndFound.setLafIcon(icon_url);
				if (sysLostAndFound != null) {
					try {
						sysLostAndFoundService.save(sysLostAndFound);
					} catch (Exception e) {
						log.info("保存失败" + e.getMessage());
						result.setMsg("保存失败！");
						result.setSuccess(1);
					}
				}
			}catch(Exception e){
				
			}
		}
				
	}
	return result;
	}

	


	/**
	 * 跳转至 sys_lost_and_found_au页
	 */
	@RequestMapping(value = "/edit")
	public String edit(String id, Map<String, Object> map) {
		log.info(id);
		map.put("saveUrl", "/manage/sysLostAndFound/editSave");
		List<SysLostAndFound> model = sysLostAndFoundService.getUser(id);
		map.put("model", model.get(0));
		List<SysDepartment> dep = this.getDepartment().getRows();
		map.put("dep", dep);
	
		return "/WEB-INF/jsp/system/system/lost_and_found/sys_lost_and_found_au.jsp";
	}
	
	
 /* 解决hibernate修改时将未修改数据清空问题*/ 

	@ModelAttribute
	public void getEmployee(@RequestParam(value="id",required=false) String id,Map<String, Object> map){
		System.out.println("ModelAttribute");
		if(id != null){
			System.out.println("ModelAttribute"+id);
			map.put("model", sysLostAndFoundService.getUser(id).get(0));
		}
	}
		
	
	/**
	 * 跳转至 页
	 */
	@ResponseBody
	@RequestMapping(value = "/editSave")
	public ReturnInfo editSave(@Valid SysLostAndFound sysLostAndFound, BindingResult b, ReturnInfo result, @RequestParam(value = "uploadFile", required = false) MultipartFile file, HttpServletRequest request) {
		if (b.getErrorCount() > 0) { // 数据校验
			log.info("数据校验出错！");
			for (FieldError error : b.getFieldErrors()) {
				log.info(error.getField() + ":" + error.getDefaultMessage());
				result.setMsg(error.getField() + "" + error.getDefaultMessage());
			}
			result.setSuccess(1);
		} else {
			if (sysLostAndFound != null) {
				try {
					String icon_url = FileUploadUtil.saveFile(file, request, ConfigProperty.getProperty("fileUplaodPath", "config") + "icon");
					sysLostAndFound.setLafIcon(icon_url);
					System.out.println(sysLostAndFound.getSysDepartment().getDepName());
					sysLostAndFoundService.update(sysLostAndFound);
					//sysCustomerService.saveOrUpdate(sysCustomer);
				} catch (Exception e) {
					log.info("修改异常" + e.getMessage());
					result.setMsg("修改失败");
					
					result.setSuccess(1);
				}

			}
		}

		return result;
	} 

	/**
	 * 物理删除,从数据库中删除
	 */
	@ResponseBody
	@RequestMapping(value = "/delete")
	public ReturnInfo delete(String ids, ReturnInfo result) {
		if (ids != null) {
			try {
				sysLostAndFoundService.deleteAll(ids);
			} catch (Exception e) {
				log.info("删除异常" + e.getMessage());
				result.setMsg("删除失败！");
				result.setSuccess(1);
			}
		}

		return result;
	} 

	/**
	 * 移除,逻辑删除，将状态修改为0
	 */
	@ResponseBody
	@RequestMapping(value = "/remove")
	public ReturnInfo remove(String ids, ReturnInfo result) {
		log.info(ids);
		if (ids != null) {
			try {
				sysLostAndFoundService.removeAll(ids);
			} catch (Exception e) {
				log.info("移除异常" + e.getMessage());
				result.setMsg("移除失败！");
				result.setSuccess(1);
			}
		}

		return result;
	} 

//	/**
//	 * 获得可用的部门(试用)
//	 */
//	@ResponseBody
//	@RequestMapping(value = "/getDep")
//	public DataGrid getDep() {
//			DataGrid dg = new DataGrid();
//			try {
//				dg.setRows(sysCustomerService.getDep());
//				
//			} catch (Exception e) {
//				log.info("异常" + e.getMessage());
//			}
//		return dg;
//	}
	
	/**
	 * 获得客户详细资料
	 */
	@RequestMapping(value = "/queryArcitleDetail")
	public String queryArcitleDetail(String id,Map<String, Object> map) {
		log.info(id);
		System.out.println("kkkkkkkkkkkkkkkkkk");
		if (id != null) {
			List<SysLostAndFound> model = sysLostAndFoundService.getUser(id);
			map.put("model", model.get(0));
		}
		return "/WEB-INF/jsp/system/system/lost_and_found/sys_lost_and_found_detail.jsp";
	} 
	/**
	 * 获取部门信息
	 */
	public DataGrid getDepartment(){
		return sysDepaermentService.datagrid();
	}
	 
}

